///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2011. Dimarik
//
///////////////////////////////////////////////////////////////////////////////

package
{

import flash.display.Sprite;
import flash.events.Event;
import flash.system.System;

/**
 *  @author				Dimarik
 *  @version			1.0
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="600", height="400", frameRate="21", backgroundColor="#E0EEEE")]

public class DisplayListGCTest extends Sprite {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function DisplayListGCTest() {
		super();
		super.addEventListener(Event.ADDED_TO_STAGE, this.creationComplete)
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	private function creationComplete(event:Event):void {
		trace('DisplayListGCTest creationComplete');

		var sprite1:Sprite = new Sprite();
		sprite1.focusRect = null;
		sprite1.mouseEnabled = false;
		super.addChild(sprite1);

		var sprite2:Sprite = new MySprite();
		sprite1.addChild(sprite2);
		
		super.stage.focus = sprite2;
		//super.addChild(sprite2);

		//super.removeChild(sprite1);

		//System.gc();
	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

}
}
//=============================================================================
//
//   Inner definitions
//
//=============================================================================

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.MouseEvent;

import mx.events.ModuleEvent;

internal class MySprite extends Sprite {
	
	public function MySprite() {
		super();
		
		const g:Graphics = this.graphics;
		g.beginFill(0xFF0000, 1);
		g.drawRect(0, 0, 100, 100);
		g.endFill();

		super.addEventListener(Event.ADDED_TO_STAGE, this.creationComplete);
	}

	private function creationComplete(event:Event):void {
		trace('MySprite creationComplete');
		//super.stage.addEventListener(MouseEvent.CLICK, this.handler_mouseClick);
		super.removeEventListener(Event.ADDED_TO_STAGE, this.creationComplete);
		super.addEventListener(Event.ACTIVATE, this.handler_activate);
		super.addEventListener(Event.DEACTIVATE, this.handler_deactivate);
		super.addEventListener(FocusEvent.FOCUS_IN, this.handler_focusIn);
		super.addEventListener(FocusEvent.FOCUS_OUT, this.handler_focusOut);
		//super.addEventListener(Event.FRAME_CONSTRUCTED, this.handler_frameConstructed);
	}

	private function handler_mouseClick(event:MouseEvent):void {
		trace('handler_mouseClick', (event.target as DisplayObjectContainer).numChildren);
	}

	private function handler_activate(event:Event):void {
		trace('handler_activate', event);
	}

	private function handler_deactivate(event:Event):void {
		trace('handler_deactivate', event);
	}

	private function handler_frameConstructed(event:Event):void {
		trace('handler_deactivate', event);
	}

	private function handler_focusIn(event:Event):void {
		trace('handler_focusIn', event);
	}

	private function handler_focusOut(event:Event):void {
		trace('handler_focusOut', event);
		//super.stage.focus = this;
	}
}